function closeConfirm (obj) {
  obj.remove()
}
function initMyConfirm (parms, callback) {
  const defaultParms = {
    title: '确认框', // 确认框标题
    type: 'confirm', // 确认框类型， 默认为confirm ，设置为warn 类型，只显示确定按钮
    msg: '确定删除该项吗？', // 确认信息
    width: 400, // 宽度
    confirmButtonText: '确定', // 确定按钮文案
    cancelButtonText: '取消' // 取消按钮文案
  }
  const finalParms = Object.assign(defaultParms, parms)
  let bodyDom = document.querySelector('body')
  let confirmDom = document.createElement('div')
  let maskDom = document.createElement('div')
  let confirmBoxDom = document.createElement('div')
  confirmDom.classList.add('p-confirm')
  maskDom.classList.add('p-confirm-container')
  confirmBoxDom.classList.add('p-confirm-content')
  confirmBoxDom.style = 'width:' + defaultParms.width + 'px'
  // 创建header
  let confirmContentHtml = '<div class="p-confirm-header">' + finalParms.title + '<span style="position:absolute; right:15px;top:10px; cursor:pointer"><i class="iconfont cancel-confirm" style="font-size:20px !important">&#xe600;</i></span></div>'
  confirmContentHtml += '<div class="p-confirm-body">' + finalParms.msg + '</div>'
  let cancelBtn = finalParms.type === 'confirm' ? '<button class="p-btn p-btn-cancel cancel-confirm">' + finalParms.cancelButtonText + '</button>' : ''
  confirmContentHtml += '<div class="p-confitm-foot" style="padding-bottom:30px">' + cancelBtn + '<button class="p-btn p-btn-primary queren-confirm">' + finalParms.confirmButtonText + '</button></div>'
  confirmBoxDom.innerHTML = confirmContentHtml
  maskDom.appendChild(confirmBoxDom)
  confirmDom.appendChild(maskDom)
  bodyDom.appendChild(confirmDom)
  // 相关点击事件
  maskDom.addEventListener('click', function (e) {
    let className = e.target.classList.value
    let returnRes = false
    // 如果点击关闭或取消按钮
    if (className.indexOf('cancel-confirm') !== -1) {
      closeConfirm(confirmDom)
    }
    // 点击确定按钮
    if (className.indexOf('queren-confirm') !== -1) {
      closeConfirm(confirmDom)
      returnRes = true
    }
    callback(returnRes)
  })
}
const minxin = {
  methods: {
    myConfirm (parms) {
      return new Promise((resolve) => {
        initMyConfirm(parms, (res) => {
          resolve(res)
        })
      })
    },
    addActive (target) {
      this.$refs[target].classList.add('validate-active')
    },
    removeActive (type) {
      if (type === 'all') {
        for (let i in this.$refs) {
          this.$refs[i].classList.remove('validate-active')
        }
      } else {
        type.forEach(item => {
          this.$refs[item].classList.remove('validate-active')
        })
      }
    },
    // 排序值校验
    sortValidate (sortNum) {
      if (!sortNum) {
        return true
      }
      if (isNaN(sortNum) || (sortNum < 0 || sortNum > 2147483647)) {
        this.$message({
          type: 'error',
          message: '排序值必须为 0~2147483647 之间的数字'
        })
        return false
      }
      return true
    },
    // 编码校验
    codeValidate (code) {
      if (code === '') {
        this.$message({
          type: 'error',
          message: '请输入编码'
        })
        return false
      }
      if (code === '') {
        this.$message({
          type: 'error',
          message: '请输入编码'
        })
        return false
      }
      let codeReg = /^\w+$/
      if (!codeReg.test(code)) {
        this.$message({
          type: 'error',
          message: '请输入数字，字母或下划线'
        })
        return false
      }
      return true
    },
    setRadomCode (type) {
      if (type.length <= 2) {
        let date = new Date().getTime().toString(36)
        return type + date
      } else {
        console.error('参数请保持两位以内')
      }
    },
    calcTime (timeUnit, startTime, expectLengthTime) {
      console.log(timeUnit, startTime, expectLengthTime)
      let endTime = 0
      switch (timeUnit) {
        case '秒': endTime = startTime + expectLengthTime * 1000
          break
        case '分钟': endTime = startTime + expectLengthTime * 60000
          break
        case '小时': endTime = startTime + expectLengthTime * 3600000
          break
        case '天': endTime = startTime + expectLengthTime * 86400000
          break
        case '周': endTime = startTime + expectLengthTime * 604800000
          break
        case '月': endTime = this.getMonthDays(startTime, expectLengthTime)
          break
        case '年': endTime = this.getYearDays(startTime, expectLengthTime)
          break
      }
      return endTime
    },
    getMonthDays (startTime, expectLengthTime) {
      let month = new Date(startTime).getMonth() + 1
      let year = new Date(startTime).getFullYear()
      let day = new Date(startTime).getDate()
      let hour = new Date(startTime).getHours()
      let min = new Date(startTime).getMinutes()
      let sec = new Date(startTime).getSeconds()
      let endmonth = month + expectLengthTime
      if (endmonth > 12) {
        year += parseInt(endmonth / 12)
        endmonth = endmonth % 12
      }

      let time = `${year}-${endmonth}-${day} ${hour}:${min}:${sec}`

      return new Date(time).getTime()
    },
    getYearDays (startTime, expectLengthTime) {
      let month = new Date(startTime).getMonth() + 1
      let year = new Date(startTime).getFullYear() + expectLengthTime
      let day = new Date(startTime).getDate()
      let hour = new Date(startTime).getHours()
      let min = new Date(startTime).getMinutes()
      let sec = new Date(startTime).getSeconds()
      let time = `${year}-${month}-${day} ${hour}:${min}:${sec}`
      return new Date(time).getTime()
    },
    calcRemindTime (timeUnit, startTime, expectLengthTime) {
      let endTime = 0
      let currentTime = new Date().getTime()
      switch (timeUnit) {
        case '秒': endTime = startTime - expectLengthTime * 1000
          break
        case '分钟': endTime = startTime - expectLengthTime * 60000
          break
        case '小时': endTime = startTime - expectLengthTime * 3600000
          break
        case '天': endTime = startTime - expectLengthTime * 86400000
          break
        case '周': endTime = startTime - expectLengthTime * 604800000
          break
        case '月': endTime = this.getReminMonths(startTime, expectLengthTime)
          break
        case '年': endTime = this.getRemindYears(startTime, expectLengthTime)
          break
      }
      if (endTime < currentTime) {
        endTime = 0
      }
      return endTime
    },
    getReminMonths (startTime, expectLengthTime) {
      let month = new Date(startTime).getMonth() + 1
      let year = new Date(startTime).getFullYear()
      let day = new Date(startTime).getDate()
      let hour = new Date(startTime).getHours()
      let min = new Date(startTime).getMinutes()
      let sec = new Date(startTime).getSeconds()
      let endmonth = month - expectLengthTime
      if (endmonth < 0) {
        year -= parseInt(endmonth / 12)
        endmonth = Math.abs(endmonth % 12)
      }
      let time = `${year}-${endmonth}-${day} ${hour}:${min}:${sec}`
      return new Date(time).getTime()
    },
    getRemindYears (startTime, expectLengthTime) {
      let month = new Date(startTime).getMonth() + 1
      let year = new Date(startTime).getFullYear() - expectLengthTime
      let day = new Date(startTime).getDate()
      let hour = new Date(startTime).getHours()
      let min = new Date(startTime).getMinutes()
      let sec = new Date(startTime).getSeconds()
      let time = `${year}-${month}-${day} ${hour}:${min}:${sec}`
      return new Date(time).getTime()
    }
  }
}
export default minxin
